package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.PddCollectionBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Hankkin on 16/1/30.
 */
public class PddCollectionAdapter extends BaseAdapter {
    private Context context;
    private List<PddCollectionBean.DataBean> dataList;
    private LayoutInflater inflater;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String maType;
    private OnItemClickListener mItemClickListener;
    public PddCollectionAdapter(Context context, List<PddCollectionBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences( "user", Activity.MODE_PRIVATE );
        editor = sp.edit();
        maType=sp.getString("SpareTwo","");
    }
    public void setDataList(List<PddCollectionBean.DataBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoloder holoder = null;
        if (convertView==null){
            holoder = new ViewHoloder();
            convertView = inflater.inflate(R.layout.product_list_item,null);
            holoder.product = (ImageView) convertView.findViewById(R.id.product);
            holoder.source = (ImageView) convertView.findViewById(R.id.source);
            holoder.price = (TextView) convertView.findViewById(R.id.price);
            holoder.before_price = (TextView) convertView.findViewById(R.id.before_price);
            holoder.num = (TextView) convertView.findViewById(R.id.num);
            holoder.quan = (TextView) convertView.findViewById(R.id.quan);
            holoder.name = (TextView) convertView.findViewById(R.id.name);
            holoder.name2 = (TextView) convertView.findViewById(R.id.name2);
            holoder.earn = (TextView) convertView.findViewById(R.id.earn);
            convertView.setTag(holoder);
        }
        else {
            holoder = (ViewHoloder) convertView.getTag();
        }

        PddCollectionBean.DataBean  item = dataList.get(position);
        if(null!=item && null !=item.getImgUrl() && null!=item.getCommission() && null!=item.getVolume() && null!=item.getCoupon() && null!=item.getTitle() && null!=item.getOriginalPrice() && null!=item.getPresentPrice()){
        DecimalFormat df = new DecimalFormat( "0.00" );
        Glide.with( context ).load(item.getImgUrl()).into( holoder.product );
        holoder.before_price.getPaint().setFlags( Paint. STRIKE_THRU_TEXT_FLAG );
        holoder.source.setImageResource(R.mipmap.pdd_source);
        holoder.before_price.setText("原价"+df.format(Double.parseDouble(item.getOriginalPrice())));
        holoder.price.setText(df.format(Double.parseDouble(item.getPresentPrice())));
        holoder.num.setText("月销:"+item.getVolume());
        holoder.quan.setText(item.getCoupon().replace(".00","")+"元券");
        if(item.getTitle().length()>16){
            holoder.name.setText(item.getTitle().substring(0,16));
            holoder.name2.setText(item.getTitle().substring(16,item.getTitle().length()));
        }else{
            holoder.name.setText(item.getTitle());
        }
        if(maType.equals("")|| maType.equals("1")){
            holoder.earn.setVisibility(View.GONE);
            SpannableString spannableString= new SpannableString(holoder.price.getText().toString());
            RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
            spannableString.setSpan(sizeSpan, 0,holoder.price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holoder.price.setText(spannableString);
        }else{
            holoder.earn.setVisibility(View.VISIBLE);
            holoder.earn.setText(item.getCommission());
        }
        SpannableString spannableString= new SpannableString(holoder.price.getText().toString());
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
        spannableString.setSpan(sizeSpan, 0,holoder.price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        holoder.price.setText(spannableString);
        }
        return convertView;
    }

    class ViewHoloder{
        ImageView product;
        ImageView source;
        TextView price;
        TextView before_price;
        TextView num;
        TextView quan;
        TextView name;
        TextView name2;
        TextView earn;
    }
}
